package com.situ.company.score.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.project.model.ProjectModel;
import com.situ.company.score.model.ScoreModel;
import com.situ.company.score.service.IScoreService;
import com.situ.company.score.service.impl.ScoreServiceImpl;
import com.situ.company.util.FmtRequest;

@WebServlet("/score")
public class ScoreServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action=req.getParameter("action");
		Object res="";
		switch(action) {
		case "page":
			res=page(req);
			break;
		case "get":
			res=get(req);
			break;
		case "upd":
			res=upd(req);
			break;
		case "del":
			res=del(req);
			break;
			
		}
		FmtRequest.write(resp.getWriter(), res);
	}
	private Object del(HttpServletRequest req) {
		ScoreModel model=FmtRequest.parseModel(req, ScoreModel.class);
		System.out.println(model.toString());
		return service.delete(model);
	}
	private Object upd(HttpServletRequest req) {
		ScoreModel model=FmtRequest.parseModel(req, ScoreModel.class);
		return service.update(model);
	}
	private Object get(HttpServletRequest req) {
		Map<String,String> prop=new HashMap<>();
		prop.put("codeEmp", "codeEmp");
		prop.put("codePro", "codePro");
		ScoreModel model=FmtRequest.parseModel(req, ScoreModel.class,prop);
		List<ScoreModel> list = service.selectList(model);
		return list.get(0);
	}
	IScoreService service =new ScoreServiceImpl();
	private Object page(HttpServletRequest req) {
		Map<String,String> prop=new HashMap<>();
		prop.put("codePro", "codePro");
		prop.put("codeEmp", "codeEmp");
		ScoreModel model=FmtRequest.parseModel(req, ScoreModel.class,prop);
		String pageIndex=req.getParameter("pageIndex");
		String pageLimit=req.getParameter("pageLimit");
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<ScoreModel> list = service.selectList(model);
		Integer count=service.selectCount(model);
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		return map;
	}
}
